﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Dora.Tools.Utility
{
    public static class StringHelper
    {

        /// <summary>
        /// 获取文件后缀
        /// </summary>
        /// <param name="fileName">文件名</param>
        public static string GetFileExt(string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                return "";
            }
            var dotIndex = fileName.LastIndexOf('.');
            if (dotIndex < 0)
            {
                return "";
            }
            var ext = fileName.Substring(dotIndex, fileName.Length - dotIndex);
            return ext.ToLower();
        }

        public static string GetFirstItemFromStr(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            var arr = str.Split(',', StringSplitOptions.RemoveEmptyEntries);
            var firstStr = arr.Length > 0 ? arr[0] : "";
            return firstStr;
        }

        /// <summary>
        /// 只保留字符串中的字母数字
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string GetNumberAlpha(string source)
        {
            string pattern = "[A-Za-z0-9]";
            string strRet = "";
            MatchCollection results = Regex.Matches(source, pattern);
            foreach (var v in results)
            {
                strRet += v.ToString();
            }
            return strRet;
        }

        /// <summary>
        /// 只保留字符串中的字母
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string GetAlpha(string source)
        {
            string pattern = "[A-Za-z]";
            string strRet = "";
            MatchCollection results = Regex.Matches(source, pattern);
            foreach (var v in results)
            {
                strRet += v.ToString();
            }
            return strRet;
        }
    }
}
